﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum ProjectileType
{
    MagicBall
}


public class Projectile : MonoBehaviour {
    public ProjectileType type;



    public ProjectileWarhead warhead;
    public Vector3 origin;
    public Transform pointingTarget;
    public Vector3 nonpointingTaget;
    public float duration;
    public float gravity = 9.8f;
    /// <summary>
    /// 追踪抛物线，和物理上的抛物线不同，这里的水平速度会不断改变速度和方向，保证能够集中目标
    /// </summary>
    public bool isArc;


    
    public float t;

    public void CloneTo(ref Projectile target)
    {
        target= MemberwiseClone() as Projectile;
    }


    private void explode()
    {
        if(warhead!=null)
        {
            warhead.explode();
        }
        ProjectileManager.Instance.Return2Pool(this.gameObject);
    }


    private void Update()
    {
        Vector3 newpos = Vector3.zero;
        t += Time.deltaTime;
        float vy0 = gravity * duration *0.5f;
        if (pointingTarget != null)
        {
            newpos = Vector3.Lerp(origin, pointingTarget.position, t / duration);
            newpos += Vector3.up * (vy0 * t - 0.5f * gravity * t * t);
            nonpointingTaget = pointingTarget.transform.position;
        }
        else if(nonpointingTaget!=Vector3.zero)
        {
            newpos = Vector3.Lerp(origin, nonpointingTaget, t / duration);
            newpos += Vector3.up * (vy0 * t - 0.5f * gravity * t * t);
        }
        else
        {
            ProjectileManager.Instance.Return2Pool(this.gameObject);
        }
        transform.position = newpos;
        if(t>duration)
        {
            explode();
        }
        
    }


}
